Welcome to JavaScript!

4.14 终止while循环

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

<script type="text/javascript">

var a=1;

var b=Math.floor(Math.random()*(20-1))+1;

alert(b);

while (a<1000){

if (a==b) {

document.write("密码"+a+"正确<br>");

break;

}

if (a==4 || a==7 || a==14) {

a++; //终止本次循环前,让变量自增;

continue; //终止本次循环,开始下次循环,后面的代码不会扫执行

}

document.write("密码"+a+"不对<br>")

a++;

};

</script>

</head>

<body>

</body>

</html>

随机取值为12则返回以下:

密码1不对

密码2不对

密码3不对

密码5不对

密码6不对

密码8不对

密码9不对

密码10不对

密码11不对

密码12正确